package abs.service;

import abs.dao.MonedasDAO;

import abs.domain.Monedas;

import java.util.List;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Monedas entities
 * 
 */

@Service("MonedasService")
@Transactional
public class MonedasServiceImpl implements MonedasService {

	/**
	 * DAO injected by Spring that manages Monedas entities
	 * 
	 */
	@Autowired
	private MonedasDAO monedasDAO;

	/**
	 * Instantiates a new MonedasServiceImpl.
	 *
	 */
	public MonedasServiceImpl() {
	}

	/**
	 * Load an existing Monedas entity
	 * 
	 */
	@Override
	@Transactional
	public Set<Monedas> loadMonedass() {
		return monedasDAO.findAllMonedass();
	}

	/**
	 * Save an existing Monedas entity
	 * 
	 */
	@Override
	@Transactional
	public void saveMonedas(Monedas monedas) {
		Monedas existingMonedas = monedasDAO.findMonedasByPrimaryKey(monedas.getCo010clca());

		if (existingMonedas != null) {
			if (existingMonedas != monedas) {
				existingMonedas.setCo010clca(monedas.getCo010clca());
				existingMonedas.setCo010mone(monedas.getCo010mone());
				existingMonedas.setCo010desc(monedas.getCo010desc());
			}
			monedas = monedasDAO.store(existingMonedas);
		} else {
			monedas = monedasDAO.store(monedas);
		}
		monedasDAO.flush();
	}

	/**
	 */
	@Override
	@Transactional
	public Monedas findMonedasByPrimaryKey(String co010clca) {
		return monedasDAO.findMonedasByPrimaryKey(co010clca);
	}

	/**
	 * Return all Monedas entity
	 * 
	 */
	@Override
	@Transactional
	public List<Monedas> findAllMonedass(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Monedas>(monedasDAO.findAllMonedass(startResult, maxRows));
	}

	/**
	 * Delete an existing Monedas entity
	 * 
	 */
	@Override
	@Transactional
	public void deleteMonedas(Monedas monedas) {
		try {
			monedasDAO.remove(monedas);
			monedasDAO.flush();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Borrado Exitoso"));
		} catch (javax.persistence.EntityExistsException e) {
			//logger.error();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("catched EntityExistsException " + e.toString()));
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("catched generic Exception " + e.getMessage()));
			//logger.error("catched generic Exception " + e.getMessage());
		}
	}

	/**
	 * Return a count of all Monedas entity
	 * 
	 */
	@Override
	@Transactional
	public Integer countMonedass() {
		return ((Long) monedasDAO.createQuerySingleResult("select count(o) from Monedas o").getSingleResult()).intValue();
	}
}
